package com.niit.spark.rdd.test

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * Date:2025/4/30
 * Author：Ys
 * Description:
 */
object SortByKeyExercise {

  def main(args: Array[String]): Unit = {
    val sparkConf = new
        SparkConf().setMaster("local[*]").setAppName("SortByKeyExercise")
    val sc = new SparkContext(sparkConf)
    sc.setLogLevel("ERROR")
    val scoresRDD = sc.parallelize(Seq(("Alice", 85), ("Bob", 90), ("Charlie",
      78), ("David", 92), ("Eve", 88)))
    //其他方式
    //val resRdd: RDD[(String, Int)] = scoresRDD.sortBy(_._2, false)

    //题目要求的方式
    val mapRdd: RDD[(Int, String)] = scoresRDD.map(line => (line._2, line._1))

    val sortRdd: RDD[(Int, String)] = mapRdd.sortByKey(false)

    val resRdd: RDD[(String, Int)] = sortRdd.map(line => (line._2, line._1))

    resRdd.collect().foreach(println)

    sc.stop()
  }

  }
